<?php

class ConfigSchemes extends Controller{

    public function __construct(){
        parent::__construct();
    }

    private function _render () {
        $this->_data['title'] = 'Gestión de esquemas';
        $this->_data['page'] = 'configSchemes';
        
        $this->_view->rendertemplate('header',$this->_data);
        $this->_view->rendertemplate('menu',$this->_data);
        $this->_view->render('config/schemes',$this->_data);
        $this->_view->rendertemplate('footer',$this->_data);
    }
    
    public function index(){
        $this->_data['page_section'] = 'main';
        $this->_data['schemes'] = $this->_model->getSchemes ();
        $this->_render();
    }

    public function schemes () {
        $schemes = $this->_model->getSchemes ();  
        $this->_view->setOutput (json_encode ($schemes));
    }
    
    public function scheme () {
        $response = Array();
        $response['result'] = 'error';
        
        if (isset ($_GET['id'])) {
            $response['result'] = 'success';
            $response['scheme'] = $this->_model->getScheme ($_GET['id']);  
        }
        
        $this->_view->setOutput (json_encode ($response));
    }
    
    public function addScheme () {
        $response = Array();
        
        if (!isset ($_POST['name']) || !isset ($_POST['description'])) {
            $response['result'] = 'error';
            $response['message'] = 'POST_PARAMS ('.json_encode ($_POST).')';
        }
        else {
            $name = $_POST['name'];
            $description = $_POST['description'];
            
            if ($this->_model->addScheme ($name, $description) == true) {
                $response['result'] = 'success';
            }
            else {
                $response['result'] = 'error';
                $response['message'] = 'SQL FAIL';
            }
        }
        
        $this->_view->setOutput (json_encode ($response));
    }
    
    public function modifyScheme () {
        $response = Array();
        
        if (!isset ($_POST['id']) || !isset ($_POST['name']) || !isset ($_POST['description'])) {
            $response['result'] = 'error';
            $response['message'] = 'POST_PARAMS ('.json_encode ($_POST).')';
        }
        else {
            $id = $_POST['id'];
            $name = $_POST['name'];
            $description = $_POST['description'];
            
            if ($this->_model->modifyScheme ($id, $name, $description) == true) {
                $response['result'] = 'success';
            }
            else {
                $response['result'] = 'error';
                $response['message'] = 'SQL FAIL';
            }
        }
        
        $this->_view->setOutput (json_encode ($response));
    }
    
    public function removeScheme () {
        $response = Array();
        
        if (!isset ($_POST['id'])) {
            $response['result'] = 'error';
            $response['message'] = 'POST_PARAMS ('.json_encode ($_POST).')';
        }
        else {
            $id = $_POST['id'];
            
            if ($this->_model->removeScheme ($id) == true) {
                $response['result'] = 'success';
            }
            else {
                $response['result'] = 'error';
                $response['message'] = 'SQL FAIL';
            }
        }
        
        $this->_view->setOutput (json_encode ($response));
    }
}

?>